//
//  RecommendViewController.m
//  TingLiFeng
//
//  Created by xinxuan on 16/1/3.
//  Copyright © 2016年 xinxuan. All rights reserved.
//

#import "RecommendViewController.h"
#import "PlayerViewController.h"
@interface RecommendViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView * tableView;

@property (nonatomic, strong) NSMutableArray *modelsArray;



@end

@implementation RecommendViewController
static NSString *ID = @"identifier";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getModel];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.tableView];
    
}


-(void)viewWillAppear:(BOOL)animated{
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"supportedInterfaceOrientations" object:self userInfo:@{@"surport" : [NSNumber numberWithBool:NO]}];
    
}

-(void)getModel{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"recomment.plist" ofType:nil];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    _modelsArray = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        
        CommenModel *model = [CommenModel new];
        model.pic = dict[@"pic"];
        model.name = dict[@"name"];
        model.centent = dict[@"centent"];
        model.period = dict[@"period"];
        model.growth = dict[@"growth"];
        model.starCount = dict[@"starCount"];
        model.personNumber = dict[@"personNumber"];
        model.time = dict[@"time"];
        model.level = dict[@"level"];
        
        [self.modelsArray addObject:model];
        
    }
    
}





#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _modelsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    BGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    cell.model = self.modelsArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 150;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommenModel * model = [CommenModel new];
    model = _modelsArray[indexPath.row];
    NSString *string1 = model.name;
    NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 2)];
    if ([string2 isEqualToString:@"音乐"]) {
        PlayerViewController * playVC = [[PlayerViewController alloc]init];
        [self.navigationController pushViewController:playVC animated:YES];
    }else{
        TrainViewController * trainVc = [[TrainViewController alloc] init];
        trainVc.model = model;

        trainVc.nameString = model.name;
        trainVc.detailString = model.centent;
        trainVc.totalTime = [model.time integerValue] * 60;
        [self.navigationController pushViewController:trainVc animated:YES];
    }
    
}


#pragma mark ---- 懒加载
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, Screen_Width, Screen_Height - 50  - 20 - 40)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        [_tableView registerNib:[UINib nibWithNibName:@"BGTableViewCell" bundle:nil] forCellReuseIdentifier:ID];
    }
    return _tableView;
}


@end
